package com.zksite.nator.server.http.initialzer;

import java.util.concurrent.TimeUnit;

import org.springframework.stereotype.Component;

import com.zksite.nator.common.context.SpringApplicationContext;
import com.zksite.nator.server.http.handler.ForwardServerHandle;

import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpRequestEncoder;
import io.netty.handler.codec.http.HttpResponseDecoder;
import io.netty.handler.timeout.IdleStateHandler;

@Component
public class ForwardServerInitialzer extends ChannelInitializer<Channel> {

	@Override
	protected void initChannel(Channel ch) throws Exception {
		ChannelPipeline pipeline = ch.pipeline();
		pipeline.addLast("http-decoder", new HttpResponseDecoder());
		pipeline.addLast(new HttpRequestEncoder());
		pipeline.addLast("httpObject", new HttpObjectAggregator(Integer.MAX_VALUE));
		pipeline.addLast("ping", new IdleStateHandler(10, 5, 0, TimeUnit.SECONDS));
		ForwardServerHandle forwardServerHandle = SpringApplicationContext.getBean(ForwardServerHandle.class);
		pipeline.addLast(forwardServerHandle);
	}

}
